package org.lazy.generate.factory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.sql.ResultSetMetaData;
import java.util.Date;
import java.util.Set;

import org.lazy.generate.bean.GeneratorDataSource;
import org.lazy.generate.bean.GeneratorEntity;
import org.lazy.generate.bean.GeneratorTable;
import org.lazy.generate.common.XmlConstants;
import org.lazy.generate.util.SQLTypesUtil;
import org.lazy.util.DateUtil;

public class EntityFactory extends FileFactory{
	
	/**
	 * 创建Entity
	 */
	public static void buildEntity(GeneratorDataSource dataSource,GeneratorEntity entity,GeneratorTable table,ResultSetMetaData metaData) throws Exception{
		if(XmlConstants.IS_NEED_FALSE.equals(entity.getIsNeed())){
			System.out.println("ignore : "+table.getTableName()+" build Entity");
			return;
		}
		String tableName = table.getTableName();
		String entityName = table.getEntityName();
		String dbType = dataSource.getDbType();
		String entityClassName = createEntityClassName(entityName, tableName);
		String targetPath = entity.getTargetPath();
		String entityPackage = entity.getTargetPackage();
		String entityFilePath = targetPath+"/"+entityPackage.replace(".", "/");
		PrintWriter pw = null;
		File file = createTargetClass(entityFilePath, entityClassName);
		try {
			pw = new PrintWriter(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		//package
		pw.println("package "+entityPackage+";\n");
		//import
		Set<String> sets = SQLTypesUtil.needImportClass(metaData,dbType);
		writeImportClass(pw, sets);
		pw.println("");
		//class
		addCommentsToTypes(pw, tableName);
		pw.println("");
		pw.println("public class "+entityClassName+" {\t");
		//fields
		writeFields(pw, metaData,dbType);	
		pw.println("");
		//get set
		writeGettersSetters(pw, metaData,dbType);
		pw.println("}");
		pw.flush();
		pw.close();
		System.out.println("success : "+entityClassName+".java");
		
	}
	


	/**
	 * 
	 * @description 属性写入
	 * @author Anan
	 * @time  2013-9-22 下午6:26:00
	 * @param pw
	 * @param metaData
	 * @throws Exception
	 */
	private static void writeFields(PrintWriter pw,ResultSetMetaData metaData,String dbType) throws Exception{
		int colum = metaData.getColumnCount();
		for (int i = 1; i <= colum; i++) {
			String pStr = ""; 
			int columnType = metaData.getColumnType(i);
			String columnName = metaData.getColumnName(i);
			String fieldName = createFieldName(columnName);
			String columnTypeName = metaData.getColumnTypeName(i);
			String fieldTypeName = SQLTypesUtil.getJavaType(columnType, columnTypeName,dbType);
			addCommentsToFields(pw, fieldName,columnName);
			pStr += "private " + fieldTypeName + " " + fieldName + ";";
			pw.println("\t" + pStr + "");
		}
		
	}
	/**
	 * 
	 * @description get、set写入
	 * @author Anan
	 * @time  2013-9-22 下午6:26:17
	 * @param pw
	 * @param metaData
	 * @throws Exception
	 */
	private static void writeGettersSetters(PrintWriter pw,ResultSetMetaData metaData,String dbType) throws Exception{
		int colum = metaData.getColumnCount();
		for (int i = 1; i <= colum; i++) {
			String getStr = "";
			String setStr = "";
			int columnType = metaData.getColumnType(i);
			String columnName = metaData.getColumnName(i);
			String columnTypeName = metaData.getColumnTypeName(i);
			String filedName = createFieldName(columnName);
			String filedTypeName =  SQLTypesUtil.getJavaType(columnType, columnTypeName,dbType);
			setStr += "public void set"
					+ filedName.substring(0, 1).toUpperCase() + ""
					+ filedName.substring(1) + "(" + filedTypeName + " "
					+ filedName + "){\n";
			setStr += "\t\tthis." + filedName + " = " + filedName
					+ ";\n\t}";
			getStr += "public " + filedTypeName + " get"
					+ filedName.substring(0, 1).toUpperCase() + ""
					+ filedName.substring(1) + "(){\n\t";
			getStr += "\treturn this." + filedName + ";\n\t}";
			addCommentsToSetMethods(pw,filedName,columnName);
			pw.println("\t" + setStr);
			addCommentsToGetMethods(pw, filedName, columnName);
			pw.println("\t" + getStr);
		}
	}
	/**
	 * 类添加注释
	 */
	private static void addCommentsToTypes(PrintWriter pw,String tableName) throws Exception{
		pw.append("/**\n");
		pw.append(" * @table "+tableName+"\n");
		pw.append(" * @author system \n");
		pw.append(" * @createTime "+DateUtil.toString(new Date(), "yyyy-MM-dd HH:mm:ss")+"\n");
		pw.append(" */");
		
	}
	/**
	 * 属性加注释
	 */
	private static void addCommentsToFields(PrintWriter pw,String filedName,String columnName) throws Exception{
		pw.append("\t/**\n");
		pw.append("\t * @column "+columnName+"\n");
		pw.append("\t */\n");
	}
	/**
	 * set方法添加注释
	 */
	private static void addCommentsToSetMethods(PrintWriter pw,String filedName,String columnName) throws Exception{
		pw.append("\t/**\n");
		pw.append("\t * The "+filedName+" set "+columnName+" value\n");
		pw.append("\t */\n");
	}
	/**
	 * get方法添加注释
	 */
	private static void addCommentsToGetMethods(PrintWriter pw,String filedName,String columnName) throws Exception{
		pw.append("\t/**\n");
		pw.append("\t * The value obtained "+filedName+"\n");
		pw.append("\t */\n");
	}
}
